import { showMessage } from '@/components/antd-comp/message'
import { useWorkFlowStore } from '@/store/workflowStore'
import { checkAllLinkConfig } from '../utils'
import { useAutoSave } from './use-save'

/**
 * 运行前检查节点连接状态
 */
export const useCheckLinkStatus = () => {
  const { flow } = useWorkFlowStore()
  const { getMergedGraph } = useAutoSave()

  const showMessageFun = (cont, type = 'error') => {
    showMessage({
      content: cont,
      type
    })
  }
  const checkLinkStatus = () => {
    const initGraph = getMergedGraph(flow.id)
    const nodes = initGraph?.nodes || []
    const edges = initGraph?.edges || []
    const msg = checkAllLinkConfig(nodes, edges)
    if (msg) {
      showMessageFun(msg)
      return false
    }
    return true
  }
  return {
    checkLinkStatus
  }
}
